package com.huawei.crm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huawei.crm.domain.Customertracehistory;
import com.huawei.crm.domain.Employee;
import com.huawei.crm.mapper.CustomertracehistoryMapper;
import com.huawei.crm.query.QueryObject;
import com.huawei.crm.service.ICustomertracehistoryService;
import com.huawei.crm.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class CustomertracehistoryServiceImpl implements ICustomertracehistoryService {

    @Autowired
    private CustomertracehistoryMapper mapper;

    public void saveOrUpdate(Customertracehistory custhis) {
        if(custhis.getId() == null){
            // 获取当前用户
            Employee currentEmp = UserContext.getCurrentEmp();
            custhis.setInputUser(currentEmp);
            custhis.setInputTime(new Date());

            mapper.insert(custhis);
        }else {
            mapper.updateByPrimaryKey(custhis);
        }
    }

    @Transactional(readOnly = true)
    public PageInfo<Customertracehistory> query(QueryObject qo) {
        // 告诉mybatis需要去哪里拼接sql
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),"his.input_time DESC");
        List<Customertracehistory> list = mapper.selectForList(qo);
        return new PageInfo<>(list);
    }
}
